package com.smit.livevideo.sample.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.smit.utils.LogUtil;

/**
 * Created by smfang on 2015/11/24.
 * SQLiteOpenHelper继承类，执行数据库指令
 */
public class DatabaseHelper extends SQLiteOpenHelper
{
    private static final String TAG = DatabaseHelper.class.getSimpleName();
    private static final String DATABASE_NAME = "icast.db";
    private static final int DATABASE_VERSION = 3;
    private static DatabaseHelper mInstance = null;
    private static String databasesDir;
    private Context context;

    /**
     * 构造函数
     * @param context 构造函数参数
     */
    public DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
        databasesDir = context.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
        createDefaultDatabase();
        getReadableDatabase();
    }

    /**
     * 单例模式
     * @param context 传递参数
     * @return DatabaseHelper
     */
    public static synchronized DatabaseHelper getInstance(Context context)
    {
        LogUtil.debug(TAG, "inner DatabaseHelper getInstance()");
        if (mInstance == null)
        {
            mInstance = new DatabaseHelper(context);
        }
        return mInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        LogUtil.debug(TAG, "inner DatabaseHelper onUpgrade() : oldVer = " + oldVersion + ", newVer = " + newVersion);
        if (oldVersion < newVersion)
        {
            updateDatabase2To3(db);
        }
    }

    private void updateDatabase2To3(SQLiteDatabase db)
    {
        LogUtil.debug(TAG, "updateDatabase2To3()");
        //添加表
        db.execSQL(SmitSqls.DVB_CHANNEL_SQL);
    }

    private void createDefaultDatabase()
    {
        String dbSysPath = databasesDir + DATABASE_NAME;
        File file = new File(dbSysPath);
        if (file.exists())
        {
            LogUtil.trace(TAG, "createDefaultDatabase file.exists!");
            return;
        }
        file.getParentFile().mkdirs();

        LogUtil.debug(TAG, "create:" + dbSysPath);
        copyDataBase(dbSysPath);
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbSysPath, null);

        // 添加需要创建的表
        db.execSQL(SmitSqls.DVB_CHANNEL_SQL);
    }

    /**
     * 将默认数据库数据copy到数据库目录
     * @Title: copyDataBase
     * @param dstPath
     * @return: void
     */
    private void copyDataBase(String dstPath)
    {
        InputStream input = null;
        OutputStream output = null;
        try
        {
            input = context.getAssets().open(DATABASE_NAME);
            output = new FileOutputStream(dstPath);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = input.read(buffer)) > 0)
            {
                output.write(buffer, 0, length);
            }

            output.flush();
            output.close();
            input.close();
        }
        catch (IOException e)
        {
            LogUtil.trace(TAG, "copyDataBase Exception!");
        }
    }
}
